package site.hanzhe.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.hanzhe.entity.MenuEntity;
import site.hanzhe.service.MenuService;
import site.hanzhe.utils.R;

import java.util.Iterator;
import java.util.List;

/**
 * 菜单管理的Controller
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    // 获取到所有的菜单并组装为tree结构响应
    @GetMapping("/get/whole/tree.json")
    public R selectList(){
        return R.success("获取菜单列表成功！").setAttribute("root", menuService.selectTree());
    }

    // 添加菜单
    @PostMapping("/save.json")
    public R insertMenu(MenuEntity entity){
        return R.success("菜单【" + entity.getName() + "】添加成功！").setAttribute("count", menuService.insertMenu(entity));
    }

    // 修改菜单
    @PostMapping("/update.json")
    public R updateMenu( MenuEntity entity ){
        return R.success("菜单编辑成功！").setAttribute("count", menuService.updateMenu(entity));
    }

    // 删除菜单
    @PostMapping("/delete.json")
    public R deleteMenu(String id){
        return R.success("菜单删除成功！").setAttribute("count", menuService.deleteMenuById(id));
    }

}
